package com.taro.apise.indexer.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

//定义一个切面
@Aspect
@Component
@Slf4j
public class TimingAspect {
    @Pointcut("@annotation(com.taro.apise.indexer.aop.Timing)")
    public void timingPointcut() {}

    @Around("timingPointcut()")
    public Object timing(ProceedingJoinPoint joinPoint) throws Throwable{
        MethodSignature signature = (MethodSignature)  joinPoint.getSignature();
        Method method = signature.getMethod();
        Timing annotation = method.getAnnotation(Timing.class);
        String value = annotation.value();
        long b = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } finally {
            long e = System.currentTimeMillis();
            double ms = e * 1.0 - b;
            double s = ms / 1000;

            log.info("{}耗时{}秒" , value , s);
        }

    }
}
